package com.monitor.controller;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.monitor.commons.HttpTookit;
import com.monitor.commons.SkJsonResult;
import com.monitor.commons.StringUtil;
import com.monitor.commons.YXDate;
import com.monitor.commons.enums.WhetherEnum;
import com.monitor.config.yunke.YunKeConfig;
import com.monitor.config.yunke.YunKeUtil;
import com.monitor.dto.UserDTO;
import com.monitor.dto.UserPageDTO;
import com.monitor.modelExt.UserExt;
import com.monitor.pubInter.IBaseService;
import com.monitor.pubInter.exception.SkException;
import com.monitor.searchExt.UserSearchExt;
import com.monitor.service.IUserService;
import com.monitor.vo.UserDetailDomainVO;
import com.monitor.vo.UserDetailVO;
import com.monitor.vo.UserDomainVO;
import com.monitor.vo.UserListVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RequestMapping("/user/")
@RestController(value = "userController")
@Slf4j
public class UserController extends ApiManageController<UserDTO, UserPageDTO, UserExt, UserSearchExt, UserDomainVO, UserDetailDomainVO, UserDetailVO, UserListVO, String> {
    @Autowired
    private IUserService userService;

    @Override
    public IBaseService<UserExt, UserSearchExt, String> getBaseService() throws SkException {
        return userService;
    }

    @Autowired
    private YunKeUtil yunKeUtil;
    @Autowired
    private YunKeConfig yunKeConfig;

    @Override
    public void searchConditionExtend(UserSearchExt search, HttpServletRequest request, HttpSession session) throws SkException {
        search.setGroupBy("user.u_id");
        super.searchConditionExtend(search, request, session);
    }

    /**
     * 设置云客用户id
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("updateUserId")
    public SkJsonResult updateUserId() throws Exception {
        userService.updateNoIdUser();
        return SkJsonResult.ok();
    }

    /**
     * 根据手机号查询员工信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("queryUserByPhone")
    public String queryUserByPhone(String phone) throws Exception {
        //定义请求路径
        String url = yunKeConfig.getUrl() + "open/user/getUserByPhone";
        //组装请求头
        long timestamp = new Date().getTime();
        Map<String, String> headsMap = yunKeUtil.getHeadMap(timestamp);
        //组装请求参数
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("phone", phone);
        String res = HttpTookit.doPostHeader(url, paramsMap, headsMap);
        JSONObject resObject = JSONObject.parseObject(res);
        if (resObject.getBoolean("success")) {
            JSONObject dataObj = resObject.getJSONObject("data");


        }
        return res;
    }


    /**
     * 获取设备信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("queryUserDevice")
    public String queryUserDevice() throws Exception {
        //定义请求路径
        String url = yunKeConfig.getUrl() + "open/user/getUserAndDeviceInfo";
        //组装请求头
        long timestamp = new Date().getTime();
        Map<String, String> headsMap = yunKeUtil.getHeadMap(timestamp);
        //组装请求参数
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("pageSize", 100);
        String res = HttpTookit.doPostHeader(url, paramsMap, headsMap);
        JSONObject resObject = JSONObject.parseObject(res);
        if (resObject.getBoolean("success")) {
            JSONObject dataObj = resObject.getJSONObject("data");
            JSONArray userArr = dataObj.getJSONArray("users");
            log.info("总用户数量:" + userArr.size());
            int bindNum = 0;
            int online = 0;
            long nowTime = new Date().getTime();

            for (Object user : userArr) {
                JSONObject userObj = (JSONObject) user;
                String imei = userObj.getString("imei");
                String lastOnlineTime = userObj.getString("lastOnlineTime");



                if (StringUtil.isNotObjEmpty(imei)) {
                    bindNum++;
                }
                if (StringUtil.isNotObjEmpty(lastOnlineTime)) {
                    long onlineTime =Long.parseLong(lastOnlineTime);
                    Calendar cal = Calendar.getInstance();
                    cal.setTimeInMillis(onlineTime);

                    String onlineTimeStr =    YXDate.getFormatDate2String(cal.getTime())        ;
                    log.info(onlineTimeStr);

                    if ((nowTime -onlineTime) < 24 * 3600 * 1000) {
                        online++;
                    }
                }
            }
            log.info("绑定数量:" + bindNum);
            log.info("在线数量:" + online);
        }
        return res;
    }
}
